import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'register_page.dart'; // 引入注册页面
import 'home_page.dart'; // 引入主页

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  String? savedUsername;
  String? savedPassword;
  bool _rememberMe = false;

  // 登录函数
  void _login(BuildContext context) {
    String username = _usernameController.text.trim();
    String password = _passwordController.text.trim();

    if (username.isEmpty || password.isEmpty) {
      ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content: Text('用户名和密码不能为空')));
      return;
    }

    if (username == savedUsername && password == savedPassword) {
      Navigator.pushReplacement(
        context,
        MaterialPageRoute(builder: (context) => Aim()), // 登录成功跳转到主页
      );
    } else {
      ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content: Text('用户名或密码错误')));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: Column(
            children: [
              Text(
                "欢迎登录智慧文旅",
                style: TextStyle(
                  fontSize: 30,
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(height: 50),
              TextField(
                controller: _usernameController,
                decoration: InputDecoration(labelText: "用户名"),
              ),
              SizedBox(height: 15),
              TextField(
                controller: _passwordController,
                obscureText: true,
                decoration: InputDecoration(labelText: "密码"),
              ),
              SizedBox(height: 15),
              Row(
                children: [
                  Checkbox(
                    value: _rememberMe,
                    onChanged: (value) {
                      setState(() {
                        _rememberMe = value!;
                      });
                    },
                  ),
                  Text("记住密码"),
                ],
              ),
              SizedBox(height: 40),
              ElevatedButton(
                onPressed: () => _login(context),
                child: Text("登录"),
              ),
              SizedBox(height: 20),
              GestureDetector(
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => RegisterPage()),
                  ).then((registeredData) {
                    if (registeredData != null) {
                      // 注册成功后，将返回的用户名和密码保存
                      setState(() {
                        savedUsername = registeredData['username'];
                        savedPassword = registeredData['password'];
                        _usernameController.text = savedUsername!;
                        _passwordController.text = savedPassword!;
                      });
                    }
                  });
                },
                child: Row(
                  children: [
                    Text("没有账号，去注册"),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
